package com.doitedu;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.List;

/**
 * @Date 2021/12/10
 * @Created by HANGGE
 * @Description
 * 监听节点数据的变化
 * shell  get  /a  watch
 */
public class C06_Watcher03 {
    public static void main(String[] args) throws  Exception{
        /**
         * 获取ZK的连接
         * 参数三  监听器    监听获取连接成功
         */
        ZooKeeper zk = new ZooKeeper(
                "linux01:2181,linux02:2181,linux03:2181",
                2000,
             null);

        List<String> children = zk.getChildren("/", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    System.out.println("/下的子节点个数拜变化_"+event.getType());
                    //重新获取列表
                    List<String> children1 = zk.getChildren("/", this);
                    System.out.println("更新后的 /下所有的节点是: "+children1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println("当前/下所有的节点是: "+children);
        Thread.sleep(Integer.MAX_VALUE);
       // zk.close();
    }
}
